<#include "/custom.include">
<#include "/java_copyright.include">
<#assign className = table.className>   
<#assign classNameLower = className?uncap_first>
<#assign actionExtension = "do">
package ${basepackage}.${namespace}.action;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.itcmor.base.common.Result;
import com.itcmor.platform.base.exception.ItcmorException;
import com.itcmor.platform.base.page.PageInfo;
import ${basepackage}.${namespace}.bo.${className}BO;
import ${basepackage}.${namespace}.vo.${className};
import com.itcmor.base.action.BaseAction;
public class ${className}Action extends BaseAction{
	@Autowired
	private ${className}BO ${classNameLower}BO;
	private ${className} ${classNameLower} = new ${className}();
	private List<${className}> ${classNameLower}List;
	
	public String list() {
		try {
			${classNameLower} = (${className}) this.initTarget(${classNameLower});
			int recordCount = ${classNameLower}BO.count(${classNameLower});
			PageInfo pageInfo = this.initPage(recordCount);
			List<${className}> list = ${classNameLower}BO.list(${classNameLower}, ops, pageInfo);
			this.createListData(list);
		} catch (ItcmorException e) {
			e.printStackTrace();
		}
		return null;
	}
	public String toUpdate${className}() {
		try {
			${classNameLower} = ${classNameLower}BO.info(${classNameLower});
			this.createData(${classNameLower});
		} catch (ItcmorException e) {
			this.afterAction(Result.erroResult(Result.QUERY_FAIL));
			e.printStackTrace();
		}
		return null;
	}

	public String saveOrUpdate${className}() {
		${classNameLower} = (${className}) this.initTarget(${classNameLower});
		
		<@generateNotPkProperties/>  
        	<#macro generateNotPkProperties>
          		<#list table.columns as column>
							<#if column.pk>
		if (StringUtils.isEmpty(${classNameLower}.get${column.columnName}())) {
							</#if>
				</#list>
			 
			try {
				${classNameLower}BO.insert(${classNameLower});
			} catch (ItcmorException e) {
				this.afterAction(Result.erroResult(Result.INSERT_FAIL));
				e.printStackTrace();
				return null;
			}
			this.afterAction(Result.successResult(Result.INSERT_SUCCESS));
		} else {
			try {
				${classNameLower}BO.update(${classNameLower});
			} catch (ItcmorException e) {
				this.afterAction(Result.erroResult(Result.UPDATE_FAIL));
				e.printStackTrace();
				return null;
			}
			this.afterAction(Result.successResult(Result.UPDATE_SUCCESS));
		}
		return null;
	}

	public String to${className}List() {

		return "to${className}List";
	}

	public String insert() {
		try {
			${classNameLower}BO.insert(${classNameLower});
		} catch (ItcmorException e) {
			e.printStackTrace();
		}
		return "${classNameLower}List";
	}

	public String update() {
		try {
			${classNameLower}BO.update(${classNameLower});
		} catch (ItcmorException e) {
			e.printStackTrace();
		}
		return "${classNameLower}List";
	}

	public String delete() {
		String[] guids = this.getGuids().split(",");
		${className} ${classNameLower} = new ${className}();
		try {
			for (String guid : guids) {
				 
	        	 
	          		<#list table.columns as column>
								<#if column.pk>
				${classNameLower}.set${column.columnName}(guid);
								</#if>
					</#list>
				</#macro>   
				${classNameLower}BO.delete(${classNameLower}, true);
			}
		} catch (ItcmorException e) {
			this.afterAction(Result.erroResult(Result.DELETE_FAIL));
			e.printStackTrace();
		}
		this.afterAction(Result.successResult(Result.DELETE_SUCCESS));
		return null;
	}

	<@generateJavaManyToOne/>
	<#macro generateJavaManyToOne>
		<#list table.importedKeys.associatedTables?values as foreignKey>
		<#assign fkSqlTable = foreignKey.sqlTable>
		<#assign fkTable    = fkSqlTable.className>
		<#assign fkPojoClass = fkSqlTable.className>
		<#assign fkPojoClassVar = fkPojoClass?uncap_first>
		@Autowired
		private ${fkPojoClass}BO ${fkPojoClass}BO; 
		 
		public String init${fkPojoClass}() {
			try {
				this.createData(${fkPojoClass}BO.list( new ${fkPojoClass}(), null));
			} catch (ItcmorException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}
		</#list>
	</#macro>
	
	public List<${className}> get${className}List() {
		return ${classNameLower}List;
	}
	public void set${className}List(List<${className}> ${classNameLower}List) {
		this.${classNameLower}List = ${classNameLower}List;
	}
	public void set${className}BO(${className}BO ${classNameLower}BO) {
		this.${classNameLower}BO = ${classNameLower}BO;
	}

	public ${className} get${className}() {
		return ${classNameLower};
	}

	public void set${className}(${className} ${classNameLower}) {
		this.${classNameLower} = ${classNameLower};
	}

}